﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using TaskKernel.Configuration;
using TaskKernel.DTO;
using TaskKernel.Domain;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IArchiveTaskView : IAbstractView
    {
        IList<ExecutorsModel> Tasks { set; }
        IList<UniversalModelDDL> Departments { set; }
        IList<UniversalModelDDL> Executors { set; }
        IList<UniversalModelDDL> Participants { set; }
        IList<UniversalModelDDL> Owners { set; }
        SearchModel SearchModel { get; }
    }

    public partial class Archive : ViewPage<IArchiveTaskView, ArchiveTaskPresenter>, IArchiveTaskView
    {
        private const int _itemIndex = 0;

        public IList<TaskListInDepartmentModel> TasksList
        {
            set
            {
                var control = ((GridView) (ExecutorsList.Items[_itemIndex].FindControl("TasksListGridView")));
                control.DataSource = value;
                control.DataBind();
            }
        }

        public bool SortDirection
        {
            get { return ViewState["sortDirectionASC"] != null ? (bool) ViewState["sortDirectionASC"] : true; }
            set { ViewState["sortDirectionASC"] = value; }
        }

        private bool TaskStatus
        {
            get { return WorkCB.Checked; }
        }

        #region IArchiveTaskView Members

        public string DepartmentName
        {
            set { Department.Text = value; }
        }

        public IList<ExecutorsModel> Tasks
        {
            set
            {
                ViewState["TasksList"] = value;
                ExecutorsList.DataSource = value;
                ExecutorsList.DataBind();
            }
            get { return (IList<ExecutorsModel>) ViewState["TasksList"]; }
        }

        public IList<UniversalModelDDL> Departments
        {
            set
            {
                ViewState["Departments"] = value;
                DepartmentDDL.DataSource = value;
                DepartmentDDL.DataBind();
            }
        }

        public IList<UniversalModelDDL> Executors
        {
            set
            {
                ViewState["Executors"] = value;
                ExecutorDDL.DataSource = value;
                ExecutorDDL.DataBind();
            }
        }

        public IList<UniversalModelDDL> Participants
        {
            set
            {
                ViewState["Participants"] = value;
                ParticipantDDL.DataSource = value;
                ParticipantDDL.DataBind();
            }
        }

        public IList<UniversalModelDDL> Owners
        {
            set
            {
                ViewState["Owners"] = value;
                OwnerDDL.DataSource = value;
                OwnerDDL.DataBind();
            }
        }

        public SearchModel SearchModel
        {
            get
            {
                var model = new SearchModel();
                model.DateCloseFrom = DateCloseFromTB.Text;
                model.DateCloseTo = DateCloseToTB.Text;
                model.DateCreateFrom = DateCreateFromTB.Text;
                model.DateCreateTo = DateCreateToTB.Text;
                model.TaskName = SubjectTB.Text;
                model.Owner = new Guid(OwnerDDL.SelectedItem.Value) == Guid.Empty ? "" : OwnerDDL.SelectedItem.Text;
                model.Executor = new Guid(ExecutorDDL.SelectedItem.Value) == Guid.Empty ? "" : ExecutorDDL.SelectedItem.Text;
                model.Participant = new Guid(ParticipantDDL.SelectedItem.Value) == Guid.Empty ? "" : ParticipantDDL.SelectedItem.Text;
                model.FileName = FileNameTB.Text;
                model.Comment = CommentTB.Text;
                model.Tag = TagTB.Text;
                model.DepartmentName = new Guid(DepartmentDDL.SelectedItem.Value) == Guid.Empty ? "" : DepartmentDDL.SelectedItem.Text;
                model.InProgressStatus = TaskStatus;
                model.Count = int.Parse(CountTask.Text);
                model.TaskGuid = !string.IsNullOrEmpty(tbTaskGuid.Text) ? new Guid(tbTaskGuid.Text) : (Guid?) null;
                model.TaskIntCode = !string.IsNullOrEmpty(tbTaskIntCode.Text) ? Int32.Parse(tbTaskIntCode.Text) : (int?) null;
                return model;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack){
                Presenter.OnViewInitialized();
                var queue = ReadSortQueueFromCookie();
                SortExecutors(queue);
            }
        }

        private void SortExecutors(Queue<AbstractSortField> queue)
        {
            if (queue.Count > 0)
                Tasks = Tasks.Select(x => x.SortByMany(new Queue<AbstractSortField>(queue))).ToList();
        }

        private Queue<AbstractSortField> ReadSortQueueFromCookie()
        {
            var sortingFields = CookieHelper.ReadCookie(TaskKernelConfiguration.ArchivePage + "ComplexSort");
            return sortingFields.ToQueue();
        }

        protected void ExecutorsList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            var elementInExecutorsList = e.Item.ItemIndex;
            var gridViewForTaskList = ((GridView) (e.Item.FindControl("TasksListGridView")));
            gridViewForTaskList.DataSource = Tasks[elementInExecutorsList].TaskList;
            gridViewForTaskList.DataBind();
        }

        protected void EnableSearchCB_CheckedChanged(object sender, EventArgs e)
        {
            SearchPanel.Visible = !SearchPanel.Visible;
        }

        protected void TasksListGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            Tasks = Tasks.Select(x => x.Sort(e.SortExpression, SortDirection)).ToList();
            SortDirection = !SortDirection;
        }

        protected void DoSearchLB_Click(object sender, EventArgs e)
        {
            Presenter.OnSearch();
        }
    }
}